<?php ob_start();session_start();?>
<?php
       include "connection.php";
	   $display=9;
		$start = isset($_GET['start']) && (int)$_GET['start']>=0 ? $_GET['start'] : 0;
			if(isset($_GET['timkiem_loai'])||isset($_GET['submit'])||(isset($_GET['txt_Search'])&&!empty($_GET['input']))||(isset($_SESSION['gia'])||isset($_SESSION['loai'])||isset($_SESSION['thuonghieu'])||isset($_SESSION['TK'])))
			{
				$dem=0;
				$gia=$_GET['timkiem_gia'];
				$thuonghieu=$_GET['timkiem_thuonghieu'];
				$loai=$_GET['timkiem_loai'];
				if(!empty($_GET['input'])&&(empty($gia)&&empty($thuonghieu)&&empty($loai)))
				{
					$TK = $_GET['input'];
					$_SESSION['TK']= $TK;
					unset($_SESSION['loai'],$_SESSION['thuonghieu'],$_SESSION['gia']);
					$sql="Select * from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
					where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and TenSanPham like '%".$TK."%'
					order by NgayNhap
					LIMIT $start,$display";//câu truy vấn lấy dữ liệu tìm kiếm	
					$sql1="Select COUNT(*) from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
					where s.MaKhuyenMai=k.MaKhuyenMai and  s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and TenSanPham like '%".$TK."%'";
				}
				else{
					if(!empty($gia)&&(empty($_GET['input'])&&empty($thuonghieu)&&empty($loai)))
					{
						$gia=$_GET['timkiem_gia'];
						$_SESSION['gia']= $gia;
						unset($_SESSION['loai'],$_SESSION['thuonghieu'],$_SESSION['TK']);
						$sql="Select * from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
					where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and DonGiaDuKien >= '".$gia."'
					order by NgayNhap
					LIMIT $start,$display";//câu truy vấn lấy dữ liệu tìm kiếm
						$sql1="Select COUNT(*) from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
					where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and DonGiaDuKien >= '".$gia."'";
					}
					else{
						if(!empty($thuonghieu)&&(empty($_GET['input'])&&empty($gia)&&empty($loai)))
						{
							$thuonghieu=$_GET['timkiem_thuonghieu'];
							$_SESSION['thuonghieu']= $thuonghieu;
							unset($_SESSION['loai'],$_SESSION['gia'],$_SESSION['TK']);
							$sql="Select * from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
						where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and s.MaThuongHieu = '".$thuonghieu."'
						order by NgayNhap
						LIMIT $start,$display";//câu truy vấn lấy dữ liệu tìm kiếm
						$sql1="Select COUNT(*) from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
						where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and s.MaThuongHieu = '".$thuonghieu."'";
						}
						else{
							if(!empty($loai)&&(empty($_GET['input'])&&empty($gia)&&empty($thuonghieu)))
							{
								$loai=$_GET['timkiem_loai'];
								$_SESSION['loai']= $loai;
								unset($_SESSION['thuonghieu'],$_SESSION['gia'],$_SESSION['TK']);
								$sql="Select * from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
							where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and s.MaLoai = '".$loai."'
							order by NgayNhap
							LIMIT $start,$display";//câu truy vấn lấy dữ liệu tìm kiếm
								$sql1="Select COUNT(*) from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
							where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and s.MaLoai = '".$loai."'";
							}
							else{
								if(empty($_GET['input'])&&(!empty($loai)&&!empty($gia)&&empty($thuonghieu)))
								{
									$loai=$_GET['timkiem_loai'];
									$gia=$_GET['timkiem_gia'];
									$_SESSION['gia']= $gia;
									$_SESSION['loai']= $loai;
									unset($_SESSION['thuonghieu'],$_SESSION['TK']);
									$sql="Select * from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
								where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and s.MaLoai = '".$loai."' and DonGiaDuKien >='".$gia."'
								order by NgayNhap
								LIMIT $start,$display";//câu truy vấn lấy dữ liệu tìm kiếm
									$sql1="Select COUNT(*) from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
							where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and s.MaLoai = '".$loai."'";
								}
								else{
									if(empty($_GET['input'])&&(!empty($loai)&&empty($gia)&&!empty($thuonghieu)))
									{
										$loai=$_GET['timkiem_loai'];
										$thuonghieu=$_GET['timkiem_thuonghieu'];
										$_SESSION['thuonghieu']= $thuonghieu;
										$_SESSION['loai']= $loai;
										unset($_SESSION['gia'],$_SESSION['TK']);
										$sql="Select * from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
									where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and s.MaLoai = '".$loai."' and s.MaThuongHieu ='".$thuonghieu."'
									order by NgayNhap
									LIMIT $start,$display";//câu truy vấn lấy dữ liệu tìm kiếm
										$sql1="Select COUNT(*) from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
									where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and s.MaLoai = '".$loai."' and s.MaThuongHieu ='".$thuonghieu."'";
									}
									else{
										if(empty($_GET['input'])&&(empty($loai)&&!empty($gia)&&!empty($thuonghieu)))
										{
											$thuonghieu=$_GET['timkiem_thuonghieu'];
											$gia=$_GET['timkiem_gia'];
											$_SESSION['gia']= $gia;
											$_SESSION['thuonghieu']= $thuonghieu;
											unset($_SESSION['loai'],$_SESSION['TK']);
											$sql="Select * from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
										where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and s.MaThuongHieu = '".$thuonghieu."' and DonGiaDuKien >='".$gia."'
										order by NgayNhap
										LIMIT $start,$display";//câu truy vấn lấy dữ liệu tìm kiếm
											$sql1="Select COUNT(*) from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
										where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and s.MaThuongHieu = '".$thuonghieu."' and DonGiaDuKien >='".$gia."'";
										}
										else{
											if(empty($_GET['input'])&&(!empty($loai)&&!empty($gia)&&!empty($thuonghieu)))
											{
												$loai=$_GET['timkiem_loai'];
												$gia=$_GET['timkiem_gia'];
												$thuonghieu=$_GET['timkiem_thuonghieu'];
												$_SESSION['gia']= $gia;
												$_SESSION['thuonghieu']= $thuonghieu;
												$_SESSION['loai']= $loai;
												unset($_SESSION['TK']);
												$sql="Select * from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
											where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and s.MaLoai = '".$loai."' and DonGiaDuKien >='".$gia."' and s.MaThuongHieu='".$thuonghieu."'
											order by NgayNhap
											LIMIT $start,$display";//câu truy vấn lấy dữ liệu tìm kiếm
												$sql1="Select COUNT(*) from sanpham s,loaisanpham l,nhomsanpham n,thuonghieu t,khuyenmai k
											where s.MaKhuyenMai=k.MaKhuyenMai and s.MaLoai=l.MaLoai and l.MaNhom=n.MaNhom and s.MaThuongHieu=t.MaThuongHieu and s.MaLoai = '".$loai."' and DonGiaDuKien >='".$gia."' and s.MaThuongHieu='".$thuonghieu."'";
											}
										}
									}
								}
							}
						}
					}
				}
				$result=mysql_query($sql) or die("<font size='+2' color='#FF0000' >chưa nhập thông tin tim kiếm</font>");
				echo"<table border='0'>";//start-xuất dữ liệu dạng bảng
		   echo"<tr>";
			while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
				$masp=$row['MaSanPham'];
				echo '<td>';
				echo '<div id="o_sanpham">';
					if($row["MaKhuyenMai"]!=1){echo '<div class="giamgia">-'.$row["HinhThuc"].'%</div>';}
					echo '<div class="hinhanh_minhhoa"><a href="../MyModel/ChiTiet.php?MSP='.$row["MaSanPham"].'"><img width="180px" height="235px"src="'.$row["HinhAnhSP"].'" alt="image" /></a></div>';
					echo '<div class="tenthuonghieu"><a href="../MyModel/ChiTiet.php?MSP='.$row["MaSanPham"].'">'.$row["TenThuongHieu"].'</a></div>';
					echo '<div class="details">';
						echo '<div class="gia"><span class="giacu">'.number_format($row['DonGiaDuKien'],3).' VNĐ</span> <br />';
						echo '<span class="giamoi">'.number_format(($row['DonGiaDuKien'] - ($row["DonGiaDuKien"]*$row["HinhThuc"])/100),3).' VNĐ</span></div>';
						echo '<a href="../MyModel/ChiTiet.php?MSP='.$row["MaSanPham"].'"><div class="xemchitiet"></div></a>';
					echo '</div>';
				echo '</div>';
				echo '</td>'; 
			$dem++;
			 if($dem%3==0)//3 ô sản phẩm sẻ xuống dòng
			 {
				echo('</tr><tr>');
			 }
		}
		echo "</table>";//end-xuất dữ liệu
		if(mysql_num_rows($result)==0)
		{
			echo '<font size="+2" color="#FF0000" >Không thấy sản phẩm phù hợp</font>';}
		}
echo '</div>
	</div>
</div>';
//tính tổng số trang cần hiển thị
if(isset($_GET['page']) && (int)$_GET['page']){
	$page=$_GET['page'];	
	}
	else{//nếu chưa xác định thì tính số trang
		$query=$sql1;
		$res= mysql_query($query) or die("Cound not select to sanpham!".mysql_error($con));
		$row=mysql_fetch_array($res);
		$record=$row[0];
		//nếu số record bé hơn số phần tử của 1 trang thì số trang = 1
		if($record > $display){
			$page = ceil($record / $display);
		}else{
			$page = 1;
		}	 
	}		
mysql_close($con);
?>
